/*
 * Android Runner is a multiplayer GPS game fully written by "Xurxo Mendez Perez"
 * 
 * Copyright (C) 2009 "Xurxo Mendez Perez"
 *   
 * This file is part of Android Runner.
 * 
 * Android Runner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Android Runner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Android Runner.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.sonxurxo.androidrunner.view.web.pages.question;

import org.apache.wicket.PageParameters;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.Radio;
import org.apache.wicket.markup.html.form.RadioGroup;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import es.sonxurxo.androidrunner.model.persistence.util.exceptions.DuplicateInstanceException;
import es.sonxurxo.androidrunner.model.service.game.to.QuestionTO;
import es.sonxurxo.androidrunner.view.web.application.AndroidRunnerApplication;
import es.sonxurxo.androidrunner.view.web.pages.BasePage;
import es.sonxurxo.androidrunner.view.web.session.AndroidRunnerSession;

/**
 * @author "Xurxo Mendez Perez"
 *
 */
public class CreateQuestionPage extends BasePage {

	public CreateQuestionPage() {
		super();
		add(new QuestionForm("questionform", new Model(new QuestionTO())));
	}
	
	public CreateQuestionPage(PageParameters pageParameters) {
		this();
	}
	
	public final class QuestionForm extends Form {

		private static final long serialVersionUID = 3466033543893750548L;

		public QuestionForm(String id, IModel model) {
            super(id, new CompoundPropertyModel(model));
            add(new TextField("question").setRequired(true));
            add(new TextField("option1").setRequired(true));
            add(new TextField("option2").setRequired(true));
            add(new TextField("option3").setRequired(true));
            RadioGroup choices = new RadioGroup("rightAnswer");
            choices.setRequired(true);
            choices.add(new Radio("1", new Model(new Integer(1))));
            choices.add(new Radio("2", new Model(new Integer(2))));
            choices.add(new Radio("3", new Model(new Integer(3))));
            add(choices);
        }

        @Override
		protected void onSubmit() {

        	QuestionTO questionTO = ((QuestionTO) getModel().getObject());
            try {
            	AndroidRunnerApplication.get().getGameService().createQuestion(questionTO);
            } catch (DuplicateInstanceException e) {
            	System.out.println("error");
            }
            AndroidRunnerSession session = AndroidRunnerSession.get();
            session.info(getLocalizer().getString(
            		"questionPage.questionCreated", CreateQuestionPage.this));
            throw new RestartResponseException(AndroidRunnerApplication.get().getHomePage());
        }
    }
	
	@Override
	protected String getTitle() {
		return getLocalizer().getString("questionPage.title", CreateQuestionPage.this);
	}
}
